﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GL.Tools;
using System.Net;
using System.IO;
using System.Xml;
using System.Text;

namespace WAD.WEB.wxpay
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String mch_appid = "wx15ba429106c24198";
            String mchid = "1299910501";
            String nonce_str = "1add1a30ac87aa2db7" + DateTime.Now.ToString("yyyyMMddHHmmss");
            String partner_trade_no = "1414659991";
            String openid = "dfgdgdgfdrdfdfvvdf";
            String check_name = "NO_CHECK";
            int amount = 100;
            String desc = "提现";
            String spbill_create_ip = "192.168.0.1";

            String notifyUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
            String key = "safds24fd1sfdsfd1s2f3ds1sf1d32sf";

            //拼接参数
            String qureyString = "amount=" + amount + "&check_name=" + check_name + "&desc=" + desc
                + "&mchid=" + mchid + "&mch_appid=" + mch_appid + "&nonce_str="
                + nonce_str + "&openid=" + openid + "&partner_trade_no=" + partner_trade_no + "&spbill_create_ip=" + spbill_create_ip + "&key=" + key;
            String sign = StringHelper.MD5Encrypt(qureyString).ToUpper();
            String xmlInfo = "<xml><mch_appid>{0}</mch_appid><mchid>{1}</mchid><nonce_str>{2}</nonce_str><partner_trade_no>{3}</partner_trade_no><openid>{4}</openid><check_name>{5}</check_name><amount>100</amount><desc>{7}</desc><spbill_create_ip>{8}</spbill_create_ip><sign>{9}</sign></xml>";
            String payInfo = new StringBuilder().AppendFormat(xmlInfo, mch_appid, mchid, nonce_str, partner_trade_no, openid, check_name, amount, desc, spbill_create_ip, sign).ToString();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers");

            request.Method = "POST";
            request.Timeout = 30 * 1000;

            //设置POST的数据类型和长度
            request.ContentType = "text/xml";
            byte[] data = System.Text.Encoding.UTF8.GetBytes(payInfo);
            request.ContentLength = data.Length;

            //往服务器写入数据
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();

            //获取服务端返回
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //获取服务端返回数据
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            String result = sr.ReadToEnd().Trim();
            sr.Close();

            Console.WriteLine(result);

            //XmlDocument doc = new XmlDocument();
            //doc.LoadXml(result);

            //String prepayId = doc.SelectNodes("/xml/prepay_id")[0].InnerText;

            //Console.WriteLine(prepayId);

            //String timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();
            //String nonceStr2 = "1add1a30ac87aa2db7" + StringHelper.GetRandomString3(14);

            //String qureyString2 = "appid=" + appid + "&noncestr=" + nonceStr + "&package=Sign=WXPay&partnerid=" + mchId + "&prepayid=" + prepayId + "&timestamp=" + timestamp;
            //sign = StringHelper.MD5Encrypt(qureyString2).ToUpper();

            //String payJson = "{ \"appid\":\""
            //    + appid + "\", \"noncestr\":\""
            //    + nonceStr2 + "\", \"package\":\"Sign=WXPay\", \"partnerid\":\""
            //    + mchId + "\", \"prepayid\":\""
            //    + prepayId + "\", \"timestamp\":\""
            //    + timestamp + "\", \"sign\":\""
            //    + sign + "\" }";

            //Response.Write(payJson);
            Response.End();
        }
    }
}